var outExc;//保存导出表格的数据串

// 日历
layui.use('laydate', function () {
    var laydate = layui.laydate;
    laydate.render({
        elem: '#leavetime'
    });
});
// 设置input点击后的边框颜色
$("input").on('focus', function () {
    $(this).css('border', '1px solid #a0cfff').parent().siblings().children('input').css('border', '1px solid #ccc');
});
$(".inp:eq(5) input").on('focus', function () {
    $(this).css('border', '1px solid #a0cfff').siblings('input').css('border', '1px solid #ccc');
});
// 设置x符号显示与隐藏
$("#leavetime").on('change', function () {
    if ($(this).val() != '') {
        $(".fa-times-circle-o").css('display', 'block');
    } else {
        $(".fa-times-circle-o").css('display', 'none');
    }
});
$("#leavetime").on('click', function () {
    if ($(this).val() != '') {
        $(".fa-times-circle-o").css('display', 'block');
    } else {
        $(".fa-times-circle-o").css('display', 'none');
    }
});
$(".fa-times-circle-o").on('click', function () {
    $("#leavetime").val('');
    $(this).css('display', 'none');
});
// 设置活动方式的下拉
var onOff = true;
$("#activity").click(function () {
    $(this).css('border', '1px solid blue');
    if (onOff) {
        $(".list_hide").slideDown(300);
        $('.one').css({
            transform: 'rotate(-180deg)',
            transition: '0.5s'
        });
        onOff = false;
    } else {
        $(".list_hide").slideUp(300);
        $('.one').css({
            transform: 'rotate(0deg)',
            transition: '0.5s'
        });
        onOff = true;
    }
});
$("#activity").focusout(function () {
    $(".list_hide").slideUp(300);
    $('.one').css({
        transform: 'rotate(0deg)',
        transition: '0.5s'
    });
    onOff = true;
});
$(".list_hide li option").on('click', function () {
    $(".list_hide").slideUp(300);
    $("#activity").val($(this).html());
    $('.one').css({
        transform: 'rotate(0deg)',
        transition: '0.5s'
    });
    onOff = true;
});
// function upDown() {
// 地区省份下拉效果
$("#region").on('click', function () {
    if (onOff) {
        $(".reg_list").slideDown(300);
        $('.two').css({
            transform: 'rotate(-180deg)',
            transition: '0.5s'
        });
        onOff = false;
    } else {
        $(".reg_list").slideUp(300);
        $('.two').css({
            transform: 'rotate(0deg)',
            transition: '0.5s'
        });
        onOff = true;
    }
});
$("#region").on('focusout', function () {
    $(".reg_list").slideUp(300);
    $('.two').css({
        transform: 'rotate(0deg)',
        transition: '0.5s'
    });
    onOff = true;
});
$(".reg_list li option").on('click', function () {
    $(".reg_list").slideUp(300);
    $("#region").val($(this).html());
    $('.two').css({
        transform: 'rotate(0deg)',
        transition: '0.5s'
    });
    onOff = true;
});
// };
// 省份
$.ajax({
    url: academyURL + ":9999/oaSystem/school",
    type: 'get',
    data: {
        "method": "province"
    },
    dataType: 'json',
    success: function (data) {
        // console.log(data);
        if (data.code == 100) {
            // upDown();
            var list = data.result;
            for (var i = 0; i < list.length; i++) {
                var oPtion = '<li value="' + list[i].id + '"><option value="' + list[i].id + '">' + list[i].name + '</option></li>';
                $(".reg_list").append(oPtion);
            }
        }
    },
    error: function (xhr, errorType) {
        console.log(errorType);
    }
});
$(".reg_list").on('click', 'option', function () {
    $("#region").val($(this).html());
    // console.log($(this).val())
});
// 市区ajax
$(".reg_list").on('click', 'option', function () {
    $("#shi").val('');
    var valId = $(this).val();
    // console.log(valId)
    $.ajax({
        url: academyURL + ":9999/oaSystem/school",
        type: 'get',
        data: {
            "method": "cityandarea",
            "parent_id": valId
        },
        dataType: 'json',
        success: function (data) {
            if (data.code == 100) {
                // console.log(data)
                var citylist = data.result;
                for (var i = 0; i < citylist.length; i++) {
                    var oPtionCity = '<li value="' + citylist[i].id + '"><option value="' + citylist[i].id + '">' + citylist[i].name + '</option></li>';
                    $(".shi_list").append(oPtionCity);
                }
            }
        },
        error: function (xhr, errorType) {
            console.log(errorType);
        }
    });
});

// 市区传值
$(".shi_list").on('click', 'li option', function () {
    $(".shi_list").empty();
    $("#shi").val($(this).html()).attr('valnum', $(this).val());
    // console.log($('.shi').attr('valnum'));
});
// 格式化日期，如月、日、时、分、秒保证为2位数
function formatNumber (n) {
    n = n.toString()
    return n[1] ? n : '0' + n;
}
// 参数number为毫秒时间戳，format为需要转换成的日期格式
function formatTime (number, format) {
    let time = new Date(number)
    let newArr = []
    let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']
    newArr.push(time.getFullYear())
    newArr.push(formatNumber(time.getMonth() + 1))
    newArr.push(formatNumber(time.getDate()))

    newArr.push(formatNumber(time.getHours()))
    newArr.push(formatNumber(time.getMinutes()))
    newArr.push(formatNumber(time.getSeconds()))

    for (let i in newArr) {
        format = format.replace(formatArr[i], newArr[i])
    }
    return format;
}
// 搜索
$("#btn_search").on('click', function () {
    // console.log($('#shi').attr('valnum'))
    $.ajax({
        url: academyURL + ':9999/oaSystem/school',
        type: 'get',
        data: {
            "method": "allschool",
            "schoolName": $("#scoolname").val(),
            "leaveTime": $("#leavetime").val(),
            "departConnection": $("#peoplename").val(),
            "empName": $("#channelpeo").val(),
            "beforeActivity": $("#activity").val(),
            "regionId": $("#shi").attr('valnum'),
            "currentPage": 1,
            "rows": 10
        },
        dataType: 'json',
        success: function (data) {
            console.log(data);
            outExc = data.result.list;
            $("#biglist tr td .list").empty();
            for (var i = 0; i < data.result.list.length; i++) {
                $("#biglist tr td .list").append('<li><table><tr><th colspan="5"><span style="display:none;">' + data.result.list[i].id + '</span>' + data.result.list[i].schoolName + '</th><th colspan="5" class="right_th"><span>渠道人员:</span>' + data.result.list[i].empName + '</th></tr><tr><td>实际报名人数:</td><td>预计报名人数:</td><td>目标学员数量:</td><td>以往培训总人数:</td><td>公司以往招生量:</td><td>活动日期:</td><td>活动方式:</td><td>离校时间:</td><td>院校联系人:</td><td>联系人电话:</td></tr><tr><td>' + data.result.list[i].realityNum + '</td><td>' + data.result.list[i].planRecruitNum + '</td><td>' + data.result.list[i].targetStudentSum + '</td><td>' + data.result.list[i].beforeStudySum + '</td><td>' + data.result.list[i].beforeRecruitNum + '</td><td>' + formatTime(data.result.list[i].activityTime,"Y-M-D") + '</td><td>' + data.result.list[i].planActivity + '</td><td>' + formatTime(data.result.list[i].leaveTime,"Y-M-D") + '</td><td>' + eval("(" + data.result.list[i].departConnection + ")").name + '</td><td>' + eval("(" + data.result.list[i].departConnection + ")").tel + '</td></tr></table></li>');
            }
        },
        error: function (xhr, errorType) {
            console.log(errorType);
        }
    })
});

var totalPage=10;
AjaxData(1);
//请求AJAX渲染数据
function AjaxData(num){
// 自动加载获取当前院校信息
$.ajax({
    url: academyURL + ':9999/oaSystem/school',
    type: 'get',
    data: {
        "method": "allschool",
        "schoolName": $("#scoolname").val(),
        "leaveTime": $("#leavetime").val(),
        "departConnection": $("#peoplename").val(),
        "empName": $("#channelpeo").val(),
        "beforeActivity": $("#activity").val(),
        "regionId": $("#shi").attr('valnum'),
        "currentPage": num,
        "rows": 10
    },
    dataType: 'json',
    success: function (data) {
        console.log(data);
        totalPage = data.result.totalPage;
        console.log(totalPage)
        // 页码
        $("#page").paging({
            nowPage: num, // 当前页码
            pageNum: data.result.totalPage, // 总页码
            buttonNum: 6, //要展示的页码数量
            canJump: 1,// 是否能跳转。0=不显示（默认），1=显示
            showOne: 0,//只有一页时，是否显示。0=不显示,1=显示（默认）
            callback: function (num) { //回调函数
                console.log(num);
                $("#biglist tr td .list").empty();
                AjaxData(num);
            }
        });

        outExc = data.result.list;
        $("#biglist tr td .list").empty();
        for (var i = 0; i < data.result.list.length; i++) {
            $("#biglist tr td .list").append('<li><table><tr><th colspan="5"><span style="display:none;">' + data.result.list[i].id + '</span>' + data.result.list[i].schoolName + '</th><th colspan="5" class="right_th"><span>渠道人员:</span>' + data.result.list[i].empName + '</th></tr><tr><td>实际报名人数:</td><td>预计报名人数:</td><td>目标学员数量:</td><td>以往培训总人数:</td><td>公司以往招生量:</td><td>活动日期:</td><td>活动方式:</td><td>离校时间:</td><td>院校联系人:</td><td>联系人电话:</td></tr><tr><td>' + data.result.list[i].realityNum + '</td><td>' + data.result.list[i].planRecruitNum + '</td><td>' + data.result.list[i].targetStudentSum + '</td><td>' + data.result.list[i].beforeStudySum + '</td><td>' + data.result.list[i].beforeRecruitNum + '</td><td>' + data.result.list[i].activityTime + '</td><td>' + data.result.list[i].planActivity + '</td><td>' + formatTime(data.result.list[i].leaveTime,"Y-M-D") + '</td><td>' + eval("(" + data.result.list[i].departConnection + ")").name + '</td><td>' + eval("(" + data.result.list[i].departConnection + ")").tel + '</td></tr></table></li>');
        }
        // li点击跳转
        $(".list").on('click', 'li', function () {
            $('#change_msg', window.parent.document).css('display', 'block');
            localStorage.setItem('thisId', $(this).children('table').children().children().children().children('span').html());
            window.location.href = 'add_school_child.html';
        });
    },
    error: function (xhr, errorType) {
        console.log(errorType);
    }
});
}
// 地区市份下拉效果
$("#shi").on('click', function () {
    if (onOff) {
        $(".shi_list").slideDown(300);
        $('.three').css({
            transform: 'rotate(-180deg)',
            transition: '0.5s'
        });
        onOff = false;
    } else {
        $(".shi_list").slideUp(300);
        $('.three').css({
            transform: 'rotate(0deg)',
            transition: '0.5s'
        });
        onOff = true;
    }
});
$("#shi").on('focusout', function () {
    $(".shi_list").slideUp(300);
    $('.three').css({
        transform: 'rotate(0deg)',
        transition: '0.5s'
    });
    onOff = true;
});
$("#add_stu").click(function () {
    window.location.href = 'add_school.html';
});
// 导入
$("#add_in").click(function() {
    var option = {
        url: academyURL + ':9999/oaSystem/school',
        type:'post',
        dataType:'json',
        enctype:"multipart/form-data",
        success:function(data) {
            console.log(data);
        },
        error:function(xhr, error){
            console.error(error);
        }
    }
    $("#formFile").ajaxSubmit(option);
});